import QtQuick 2.8

import "."
import "_Global"
import "_Global/Button"

import "_Global/Global.js" as GlobalJS
import "GameJS.js" as GameJS

Item {
    //property bool bRereshedRanking: false
    signal s_close()

    id: root
    visible: true
    anchors.fill: parent


    function refreshRanking() {
        //if(forceRefresh == false && bRereshedRanking == true)
        //    return;
        //bRereshedRanking = true;

        var listUserGameRanking = UserInfo.rankingInfo.listUserGameRanking(_private.nShowType);
        _private.nShowMaxPage = (listUserGameRanking.length + 19) / 20;

        //i为行数,j为记录下标
        for(var i = 0; i < _private.nShowRowCount; i++) {
            var j = i + _private.nShowPage * _private.nShowRowCount;
            if(j < listUserGameRanking.length) {

                var levelInfo = UserInfo.sl_getUserLevelInfo(listUserGameRanking[j].score);

                //if(listUserGameRanking[i].type == _private.nShowType) {
                    if(i < 3) { //前3行
                        if(j < 3) { //前3调记录
                            _private.listRowData[i].col0.text = "";
                            imageBack.imageRanking[i].visible = true;
                        }
                        else {      //不是前3调记录(非0页)
                            _private.listRowData[i].col0.text = j + 1;
                            imageBack.imageRanking[i].visible = false;
                        }
                    }
                    else {
                        _private.listRowData[i].col0.text = j + 1;
                    }
                    _private.listRowData[i].col1.text = listUserGameRanking[j].nickName;
                    _private.listRowData[i].col2.text = listUserGameRanking[j].scoreTotal;
                    _private.listRowData[i].col3.text = listUserGameRanking[j].score;
                    _private.listRowData[i].col4.source = Global._FixResourcePath_R(qsTr("Media/Images/Levels/%1.png").arg(levelInfo.level));
                    _private.listRowData[i].col5.text = (levelInfo.name == "无" ? "无" : "");
                    //_private.listRowData[i].col1.text = listUserGameRanking[i].userGameID;
                    //_private.listRowData[i].col2.text = ;
                //}
            }
            else {
                if(i < 3) {
                    imageBack.imageRanking[i].visible = false;
                }
                _private.listRowData[i].col0.text = "";
                _private.listRowData[i].col1.text = "";
                _private.listRowData[i].col2.text = "";
                _private.listRowData[i].col3.text = "";
                _private.listRowData[i].col4.source = "";
                _private.listRowData[i].col5.text = "";
            }
        }
    }

    Rectangle {
        id: background
        color: "#00ffffff"
        anchors.rightMargin: 0
        anchors.bottomMargin: 0
        anchors.leftMargin: 0
        anchors.topMargin: 0
        anchors.fill: parent

        MouseArea { //防穿透
            anchors.fill: parent
        }

        Image {
            id: imageBack
            source: Global._FixResourcePath_R("Media/Images/Ranking/Back.png")
            anchors.centerIn: parent
            anchors.fill: parent
            property var imageRanking: [imageFirst,imageSecond,imageThird]

            Image {
                id: imageBack1
                source: {
                    switch(_private.nShowType) {
                    case 1:
                        return Global._FixResourcePath_R("Media/Images/Ranking/YesterdayList.png");
                    case 2:
                        return Global._FixResourcePath_R("Media/Images/Ranking/WeekList.png");
                    }


                }
                x: Global.dpW(184)
                y: Global.dpH(101)
                width: Global.dpW(1382)
                height: Global.dpH(841)
            }

            Image {
                id: imageFirst
                source: Global._FixResourcePath_R("Media/Images/Ranking/First.png")
                x: Global.dpW(196)
                y: Global.dpH(141)
                width: Global.dpW(50)
                height: Global.dpH(40)
                z: 100
            }

            Image {
                id: imageSecond
                source: Global._FixResourcePath_R("Media/Images/Ranking/Second.png")
                x: Global.dpW(196)
                y: Global.dpH(181)
                width: Global.dpW(50)
                height: Global.dpH(40)
                z: 100
            }

            Image {
                id: imageThird
                source: Global._FixResourcePath_R("Media/Images/Ranking/Third.png")
                x: Global.dpW(196)
                y: Global.dpH(221)
                width: Global.dpW(50)
                height: Global.dpH(40)
                z: 100
            }

            ImageButton2 {
                id: buttonYesterday
                x: Global.dpW(330)
                y: Global.dpH(22)
                width: Global.dpW(260)
                height: Global.dpH(73)

                soundEffectClick: Global.resource.effects.effectButton
                bEffectOn: Global.config.bEffectOn

                source: Global._FixResourcePath_R("Media/Images/Ranking/YesterdayButton.png")

                onButtonClicked: {
                    _private.nShowType = 1;
                    _private.nShowPage = 0;
                    refreshRanking();
                }
            }


            ImageButton2 {
                id: buttonLastWeek
                x: Global.dpW(1166)
                y: Global.dpH(22)
                width: Global.dpW(260)
                height: Global.dpH(73)

                soundEffectClick: Global.resource.effects.effectButton
                bEffectOn: Global.config.bEffectOn

                source: Global._FixResourcePath_R("Media/Images/Ranking/WeekButton.png")

                onButtonClicked: {
                    _private.nShowType = 2;
                    _private.nShowPage = 0;
                    refreshRanking();
                }
            }

            ImageButton2 { //上一页
                id: buttonPrior
                x: Global.dpW(45)
                y: Global.dpH(448)
                width: Global.dpW(112)
                height: Global.dpH(111)
                source: Global._FixResourcePath_R("Media/Images/Class/Prior.png")

                soundEffectClick: Global.resource.effects.effectPage
                bEffectOn: Global.config.bEffectOn

                onButtonClicked: {
                    if(_private.nShowPage > 0) {
                        _private.nShowPage--;
                        refreshRanking();
                    }
                }
            }
            ImageButton2 { //下一页
                id: buttonNext
                x: Global.dpW(1600)
                y: Global.dpH(448)
                width: Global.dpW(112)
                height: Global.dpH(111)
                source: Global._FixResourcePath_R("Media/Images/Class/Next.png")

                soundEffectClick: Global.resource.effects.effectPage
                bEffectOn: Global.config.bEffectOn

                onButtonClicked: {
                    if(_private.nShowPage < _private.nShowMaxPage - 1) {
                        _private.nShowPage++;
                        refreshRanking();
                    }
                }
            }

            ImageButton2 {
                id: buttonClose        //关闭
                x: Global.dpW(1666)
                y: Global.dpH(22)
                width: Global.dpW(60)
                height: Global.dpH(60)

                soundEffectClick: Global.resource.effects.effectClose
                bEffectOn: Global.config.bEffectOn

                source: Global._FixResourcePath_R("Media/Images/Class/ClassInfo/Close.png")

                onButtonClicked: {
                    s_close()
                }
            }
        }
    }

    Component {
        id: rowInfo
        Item {
            property int rowY: 0
            property alias col0: col0
            property alias col1: col1
            property alias col2: col2
            property alias col3: col3
            property alias col4: col4
            property alias col5: col5
            Text {
                id: col0
                x: Global.dpW(196)
                y: Global.dpH(141 + rowY * 40)
                width: Global.dpW(50)
                height: Global.dpH(40)
                color: "white"
                font {
                    //family: ""
                    pixelSize: Global.dpH(20)
                }
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }
            Text {
                id: col1
                x: Global.dpW(265)
                y: Global.dpH(141 + rowY * 40)
                width: Global.dpW(324)
                height: Global.dpH(40)
                color: "white"
                font {
                    //family: ""
                    pixelSize: Global.dpH(20)
                }
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }
            Text {
                id: col2
                x: Global.dpW(590)
                y: Global.dpH(141 + rowY * 40)
                width: Global.dpW(324)
                height: Global.dpH(40)
                color: "white"
                font {
                    //family: ""
                    pixelSize: Global.dpH(20)
                }
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }
            Text {
                id: col3
                x: Global.dpW(915)
                y: Global.dpH(141 + rowY * 40)
                width: Global.dpW(324)
                height: Global.dpH(40)
                color: "white"
                font {
                    //family: ""
                    pixelSize: Global.dpH(20)
                }
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }
            Image {
                id: col4
                x: Global.dpW(1345)
                y: Global.dpH(141 + rowY * 40)
                width: Global.dpW(116)
                height: Global.dpH(40)
            }
            Text {
                id: col5
                x: Global.dpW(1345)
                y: Global.dpH(141 + rowY * 40)
                width: Global.dpW(116)
                height: Global.dpH(40)
                color: "white"
                font {
                    //family: ""
                    pixelSize: Global.dpH(20)
                }
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }

        }
    }

    QtObject {  //私有数据,函数,对象等
        id: _private
        property var listRowData: []
        property int nShowType: 1
        property int nShowPage: 0
        property int nShowMaxPage: 1
        property int nShowRowCount: 20
    }


    Component.onCompleted: {
        for(var i = 0; i < 20; i++) {
            _private.listRowData.push(
                        rowInfo.createObject(background,
                                             {
                                                 "rowY": i
                                             })
                        );
        }

    }

}
